import java.util.Stack;

public class DailyTemperatures {
    // leetcode 每日温度
    // https://leetcode.cn/problems/daily-temperatures/description/?envType=study-plan-v2&envId=top-100-liked
    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();
        stack.add(0);
        int len = temperatures.length;
        int[] ret = new int[len];
        for (int i = 1; i < len; i++) {
            if (temperatures[i] > temperatures[stack.peek()]) {
                while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                    int index = stack.pop();
                    ret[index] = i - index;
                }
            }
            stack.add(i);
        }
        return ret;
    }

    public static void main(String[] args) {
        DailyTemperatures dt = new DailyTemperatures();
        int[] arr = {73,74,75,71,69,72,76,73};
        int[] ret = dt.dailyTemperatures(arr);
    }
}
